package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-23 13:36
 **/
public class LeetCode3 {

    class Solution {
        public int lengthOfLongestSubstring(String s) {
            Set<Character> set = new HashSet<>();
            int len = s.length();
            int slow = 0;
            int fast = 0;
            //存储最长子串的长度
            int max = Integer.MIN_VALUE;
            while (fast < len) {
                if (!set.contains(s.charAt(fast))) {
                    max = Math.max(max, fast - slow + 1);
                    set.add(s.charAt(fast));
                    fast++;
                    continue;
                }
                while (set.contains(s.charAt(fast))) {
                    set.remove(s.charAt(slow));
                    slow++;
                }
                set.add(s.charAt(fast));
                fast++;
            }
            return max == Integer.MIN_VALUE ? 0 : max;

        }
    }

}
